﻿
namespace Sai.Core.Reporting
{
    using System.IO;
    using System.Xml.Serialization;

    public static class ObjectFlattener<T> where T : new()
    {
        public static T Inflate(string text)
        {
            T instance = default(T);

            using (var reader = new StringReader(text))
            {
                instance = Inflate(reader);
            }

            return instance;
        }

        public static T Inflate(TextReader reader)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            return (T) serializer.Deserialize(reader);
        }

        public static string Flatten(T instance)
        {
            string output = string.Empty;

            using (var writer = new StringWriter())
            {
                Flatten(instance, writer);
                output = writer.ToString();
            }

            return output;
        }

        public static void Flatten(T instance, TextWriter writer)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            serializer.Serialize(writer, instance);
        }

    }
}
